Passed
Push — master ( bd926c...e51e0c )
by Barry
02:26
created

processFile.js ➔ getCliCommand   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
const debug = require('debug')('mdprepare:processFile')
2
const {execSync} = require('child_process')
3
const fs = require('fs')
4
const os = require('os')
5
const path = require('path')
6
const h = require('./helpers.js')
7
8
export default function (filePath, clear) {
9
  // this function accepts a markdown filename and runs any mdprepare code which is embedded in that file
10
  debug(filePath)
11
  try {
12
    var txt = fs.readFileSync(filePath, 'utf8')
13
  } catch (err) {
14
    throw Error('file not found')
15
  }
16
  try {
17
    let p = processText(txt, clear, path.dirname(path.resolve(process.cwd(), filePath)))
18
    fs.writeFileSync(filePath, p)
19
  } catch (err) {
20
    throw Error('unable to write to file')
21
  }
22
  return true
23
}
24
25
export function processText (txt, clear, fileDirName) {
26
  let posn = 0
27
  let r = ''
28
  let x
29
  let y
30
  let t
31
  let frm
32
  while (true) {
33
    x = h.findMdpCode(txt, posn)
34
    y = h.findMdpInsert(txt, posn)
35
    t = h.earlierOf(x, y)
36
    if (t.start === -1) {
37
      r = r + txt.substring(posn)
38
      break
39
    } else {
40
      r = r + txt.substring(posn, t.internalStart)
41
      frm = t.internalStart + t.internalLength
42
      analyseCommandString(t)
43
      if (t.cliCommand === 'ERROR: mdpInsert command not found') {
44
        // the mdpInsert command was not present so we don't insert or remove anything, just leave as is
45
        r = r + txt.substr(t.internalStart, t.internalLength)
46
      } else {
47
        if (clear !== true) {
48
          r = r + t.prepend + runCliCmd(t.info.cliCommand, fileDirName) + t.postpend
49
        } else {
50
          // we are clearing any content so remove all lines between the start and end lines
51
          if (txt.substr(frm, 1) === '\r') { frm++ }
52
          /* istanbul ignore else  */
53
          if (txt.substr(frm, 1) === '\n') { frm++ }
54
        }
55
      }
56
      posn = t.start + t.length
57
      r = r + txt.substring(frm, posn)
58
    }
59
  }
60
  return r
61
}
62
63
function analyseCommandString (t) {
64
  // looks at the command string, checks it is a valid mdpInsert and populates .cli, .prepend and .postpend
65
  let x = t.commandString.indexOf('mdpInsert ')
66
  if (x === -1) {
67
    t.cliCommand = 'ERROR: mdpInsert command not found'
68
    return
69
  }
70
  let regex = /mdpInsert ((`{3,}|~{3,})[^ ]*) /
71
  let regexResult = regex.exec(t.commandString)
72
  if (regexResult === null) {
73
    t.info.cliCommand = t.commandString.substr(x + 10)
74
    t.prepend = ''
75
    t.postpend = ''
76
  } else {
77
    t.info.cliCommand = t.commandString.substr(x + regexResult[0].length)
78
    t.prepend = regexResult[1] + t.info.endOfLine
79
    t.postpend = t.info.endOfLine + regexResult[2]
80
  }
81
}
82
83
export function runCliCmd (str, wDirName) {
84
  // wDirName is either an absolute path or a relative path to the directory node was run from
85
  let cmd = crossPlatformCmds(str)
86
  try {
87
    return execSync(cmd, {cwd: wDirName})
88
  } catch (err) {
89
    return 'ERROR: ' + err.message
90
  }
91
}
92
93
function crossPlatformCmds (cmd) {
94
  // does limited cross platform translation of limited commands
95
  let r = cmd
96
  /* istanbul ignore if  */
97
  if (os.platform() === 'win32') {
98
    if (cmd.substr(0, 4) === 'cat ') {
99
      r = r.replace('cat ', 'type ')
100
      r = r.replace(/\//g, '\\')
101
    }
102
  }
103
  return r
104
}
105